﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Furion;
using Furion.DynamicApiController;
using Furion.UnifyResult;
using ADSTS_SERVER.Models;
using ADSTS_SERVER.ViewModels.Project;
using ADSTS_SERVER.ViewModels.ProjectTestResult;
using ADSTS_SERVER.ViewModels.Robot;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;

namespace ADSTS_SERVER.Controllers
{
    /// <summary>
    /// 工程测试结果
    /// </summary>
    [DynamicApiController]
    public class LnsProjectTestResultController
    {
        private readonly ILogger<LnsProjectTestResultController> logger;
        private readonly ISqlSugarClient db; // 核心对象：拥有完整的SqlSugar全部功能
        public LnsProjectTestResultController(ISqlSugarClient sqlSugarClient)
        {
            this.db = sqlSugarClient;
            this.logger = logger;
        }

        /// <summary>
        /// 工程列表
        /// </summary>
        /// <param name="ptrsf"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> List([FromBody] ProjectTestResultSearchForm ptrsf)
        {
            RefAsync<int> total = 0;
            List<ProjectTestResult> projects = await db.Queryable<ProjectTestResult>()
                .Mapper(pj => pj.User, pj => pj.UId, pj => pj.User.Id)
                .Where(pj => pj.IsActive == 1)
                .WhereIF(ptrsf.UId != 0, pj => pj.UId == ptrsf.UId)
                .ToPageListAsync(ptrsf.PageIndex, ptrsf.PageSize, total);

            UnifyContext.Fill(new { Total = total.Value });
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = projects,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 增加工程
        /// </summary>
        /// <param name="ptrsf"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Save([FromBody] ProjectTestResultSaveForm ptrsf)
        {
            await db.Insertable<ProjectTestResult>(ptrsf.Adapt<ProjectTestResult>()).ExecuteCommandAsync();

            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = ptrsf,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 删除工程
        /// </summary>
        /// <param name="ptrdf"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Delete([FromBody] ProjectTestResultDeleteForm ptrdf) {
            await db.Updateable<ProjectTestResult>(new ProjectTestResult { IsActive = 0 }).UpdateColumns(pj => pj.IsActive).Where(pj => pj.Id == ptrdf.Id).ExecuteCommandAsync();

            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = null,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 查看工程
        /// </summary>
        /// <param name="ptrsf"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Show([FromBody] ProjectTestResultShowForm ptrsf) {
            ProjectTestResult project = await db.Queryable<ProjectTestResult>()
                .InSingleAsync(ptrsf.Id);
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = project,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

    }
}
